﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"

namespace wxd
{
	//静态成员初始化
	const size_t string::npos = -1;

	//扩容
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		//扩容
		if (_size == _capacity)
		{
			reserve(_capacity == 0?4:2*_capacity);
		}

		//插入
		_str[_size++] = ch;
		//别忘了\0
		_str[_size] = 0;
	}
	void string::append(const char* str)
	{
		assert(str != nullptr);
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			//大于二倍有多少扩多少，小于二倍扩二倍
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}
		//插入
		strcpy(_str + _size, str);
		_size = _size + len;
	}
	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		assert(str != nullptr);
		append(str);

		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		//等于size相当于尾插
		assert(pos <= _size);
		//扩容
		if (_size == _capacity)
		{
			reserve(_size == 0 ? 4 : 2 * _capacity);
		}
		//移动
		//最好以\0的后一个为end，依次把前面赋值给后面，这样可以确保end不为负数
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}
	void string::insert(size_t pos, const char* str)
	{
		assert(str != nullptr && pos <= _size);

		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
		}
		//从前往后移动到size+len的位置上
		size_t end = _size + len;
		while (end - len + 1 > pos)
		{
			_str[end] = _str[end - len];
			end--;
		}

		memcpy(_str + pos, str, len);

		_size += len;
	}


	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		//如果写了一个超过_size - pos的值（不写缺省值会让它超过）
		//那么就直接默认pos及其后全删
		if (len > _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			//size_t end = pos + len;
			////移动
			//for (int i = 0; i < _size - pos; i++)
			//{
			//	_str[end - len + i] = _str[end + i];
			//}

			for (size_t start = pos + len; start <= _size; start++)
			{
				_str[start - len] = _str[start];
			}
			_size -= len;
		}
		
	}

	size_t string::find(char ch,size_t pos)const
	{
		assert(pos < _size);

		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}

		return npos;
	}

	//查找字符串 附上起始查找点
	size_t string::find(const char* str, size_t pos)const
	{
		assert(str && pos < _size);

		const char* s = strstr(_str+pos,str);
		if (s == nullptr)
		{
			return npos;
		}
		else
		{
			return s - _str;
		}
	}

	string string::substr(size_t pos, size_t len)const
	{
		assert(pos < _size);

		//修正len
		if (len > _size-pos)
		{
			len = _size - pos;
		}
		string sub;
		size_t i = 0;
		//防止频繁扩容，还是先扩容一下
		sub.reserve(len);
		while (len--)
		{
			sub += _str[pos + (i++)];
		}
		//传值返回会产⽣⼀个临时对象调⽤拷⻉构造
		return sub;
	}

	bool operator>(const string& str1, const string& str2) 
	{
		//类外无法直接访问私有，可以调用c_str
		return strcmp(str1.c_str(), str2.c_str()) > 0;
	}
	bool operator==(const string& str1, const string& str2)
	{
		return strcmp(str1.c_str(), str2.c_str()) == 0;
	}
	bool operator<(const string& str1, const string& str2)
	{
		return !(str1 > str2 || (str1 == str2));
	}
	bool operator>=(const string& str1, const string& str2)
	{
		return !(str1 < str2);
	}
	bool operator<=(const string& str1, const string& str2)
	{
		return !(str1 > str2);
	}
	
	bool operator!=(const string& str1, const string& str2)
	{
		return !(str1 == str2);
	}

	ostream& operator<<(ostream& out, const string& str)
	{
		for (auto ch : str)
		{
			out << ch;
		}
		//out << str.c_str();
		return out;
	}

	istream& operator>>(istream& in, string& str)
	{
	/*	char arr[10];
		in >> arr;
		str += arr;*/
		//一个字符一个字符写
		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			str += ch;
			ch = in.get();
		}

		return in;
	}

	istream& getline(istream& in, string& str)
	{
		char ch;
		ch = in.get();
		while (ch != '\n')
		{
			str += ch;
			ch = in.get();
		}

		return in;
	}
}